package marvin.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;

import java.awt.CardLayout;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;

import java.awt.Font;

import javax.swing.JTabbedPane;
import javax.swing.JRadioButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUid;
	private JPasswordField txtPwd;
	private JTextField textField;
	private final CardLayout cards =
			new CardLayout( 0, 0 );  //1
	private JPanel myPane = new JPanel();
	private JTabbedPane mainPane = 
			new JTabbedPane(JTabbedPane.TOP);
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AComp dialog = new AComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AComp() {
		setTitle(" :: 고급 컴퍼넌트 :: ");
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel top = new JPanel();
			top.setPreferredSize(new Dimension(10, 120));
			contentPanel.add(top, BorderLayout.NORTH);
			top.setLayout(new BorderLayout(0, 0));
			{
				JLabel topLogo = new JLabel("-");
				topLogo.setPreferredSize(new Dimension(395, 15));
				topLogo.setIcon(new ImageIcon(AComp.class.getResource("/marvin/swing/images/background/top.jpg")));
				top.add(topLogo, BorderLayout.WEST);
			}
			{
				final JPanel cardPane = new JPanel();
				cardPane.setPreferredSize(new Dimension(220, 10));
				top.add(cardPane, BorderLayout.EAST);
				cardPane.setLayout( cards ); // 2
				
				JPanel loginPane = new JPanel();
				cardPane.add(loginPane, "login");
				
				txtUid = new JTextField();
				txtUid.setColumns(10);
				
				JButton loginBtn = new JButton("로 그 인");
				loginBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String muid = txtUid.getText();
						String mpwd = txtPwd.getText();
						// uid라는 textfield에 입력된 문자열을 가져와서
						// 문자열 변수 muid에 저장
						if ( muid.equals("")||mpwd.equals("") )
							JOptionPane.showMessageDialog
							(null, "아이디나 비밀번호를 입력하세요!!","Message",JOptionPane.ERROR_MESSAGE);
						else if( muid.equals("abc") 
								&& mpwd.equals("12345")){
							cards.show(cardPane,"logout");
						// cards : 카드레이아웃
						// cardPane : 카드레이아웃이 적용된 패널
						// logout : cardPane에 보여줄 패널이름
							//myPane.setEnabled(false);
							mainPane.setEnabledAt(2, true);
						// 탭 활성/비활성은
						// setEnabledAt()을 이용
						// 3번째 탭을 비활성함(0,1,2 순으로 시작)
						}
						else
							JOptionPane.showMessageDialog
							(null, "아이디와 비밀번호를 확인하세요!!","Message",JOptionPane.ERROR_MESSAGE);
					}
				});
				loginBtn.setFont(new Font("굴림", Font.BOLD, 12));
				
				JButton joinBtn = new JButton("회원가입");
				joinBtn.setFont(new Font("굴림", Font.BOLD, 12));
				
				txtPwd = new JPasswordField();
				GroupLayout gl_loginPane = new GroupLayout(loginPane);
				gl_loginPane.setHorizontalGroup(
					gl_loginPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_loginPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_loginPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(txtPwd)
								.addComponent(txtUid, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_loginPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(joinBtn)
								.addComponent(loginBtn, GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE))
							.addContainerGap())
				);
				gl_loginPane.setVerticalGroup(
					gl_loginPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_loginPane.createSequentialGroup()
							.addGap(26)
							.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(loginBtn)
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(joinBtn))
							.addContainerGap(38, Short.MAX_VALUE))
				);
				loginPane.setLayout(gl_loginPane);
				{
					JPanel logoutPane = new JPanel();
					cardPane.add(logoutPane, "logout");
					
					JButton logoutBtn = new JButton("로그아웃");
					logoutBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							cards.show(cardPane, "login");
							mainPane.setEnabledAt(2, false);
						}
					});
					logoutBtn.setFont(new Font("굴림", Font.BOLD, 12));
					
					JLabel myAvatar = new JLabel("-");
					myAvatar.setIcon(new ImageIcon(AComp.class.getResource("/marvin/swing/images/img/icon2.png")));
					
					JLabel myNick = new JLabel("천송이");
					myNick.setFont(new Font("맑은 고딕", Font.BOLD, 14));
					GroupLayout gl_logoutPane = new GroupLayout(logoutPane);
					gl_logoutPane.setHorizontalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addGap(24)
								.addComponent(myAvatar, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(56)
										.addComponent(myNick))
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(36)
										.addComponent(logoutBtn)))
								.addGap(220))
					);
					gl_logoutPane.setVerticalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addContainerGap(10, Short.MAX_VALUE)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(Alignment.TRAILING, gl_logoutPane.createSequentialGroup()
										.addGroup(gl_logoutPane.createParallelGroup(Alignment.BASELINE)
											.addComponent(myNick)
											.addComponent(myAvatar, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE))
										.addGap(66))
									.addGroup(Alignment.TRAILING, gl_logoutPane.createSequentialGroup()
										.addComponent(logoutBtn)
										.addGap(48))))
					);
					logoutPane.setLayout(gl_logoutPane);
				}
			}
		}
		
		
		contentPanel.add(mainPane, BorderLayout.CENTER);
		
		JPanel basicFind = new JPanel();
		mainPane.addTab("기본검색", new ImageIcon(AComp.class.getResource("/marvin/swing/images/menu/new.png")), basicFind, null);
		
		JButton btnNewButton = new JButton("New button");
		basicFind.add(btnNewButton);
		
		JPanel detailFind = new JPanel();
		mainPane.addTab("상세검색", new ImageIcon(AComp.class.getResource("/marvin/swing/images/menu/close.png")), detailFind, null);
		
		textField = new JTextField();
		detailFind.add(textField);
		textField.setColumns(10);
		
		
		FlowLayout fl_myPane = (FlowLayout) myPane.getLayout();
		mainPane.addTab("My page", new ImageIcon(AComp.class.getResource("/marvin/swing/images/menu/exit.png")), myPane, null);
		mainPane.setEnabledAt(2, false);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
		myPane.add(rdbtnNewRadioButton);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("OK");
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
	}
}
